package com.situ.hotel.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.situ.hotel.entity.Admin;
import com.situ.hotel.entity.Result;
import com.situ.hotel.service.AdminService;

@RestController
@RequestMapping("/api/admin")
public class AdminController {

	@Autowired
	private AdminService adminService;

	@PostMapping("/login")
	public Result login(Admin admin, HttpSession session) {
		try {
			admin = adminService.login(admin);
			session.setAttribute("admin", admin);
			return Result.success(1, admin);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result.error(-1, e.getMessage());
		}
	}

	@GetMapping("/logout")
	public Result logout(HttpSession session) {
		session.invalidate();//清空Session 中的数据.
		return Result.success(1, null);
	}
	
	@GetMapping("/loginAdmin")
	public Result get(HttpSession session) {
		Admin admin = (Admin)session.getAttribute("admin");
		if(admin!=null) {
			return Result.success(1, admin);
		}else {
			return Result.error(-1, "当前无登录信息");
		}
	}
}
